package com.ruoyi.energy.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.energy.domain.NyTrans;
import com.ruoyi.energy.domain.NyTransLoad;
import com.ruoyi.energy.domain.vo.NyTransLoadWXVO;
import com.ruoyi.energy.mapper.NyTransLoadMapper;
import com.ruoyi.energy.mapper.NyTransMapper;
import com.ruoyi.energy.service.INyTransLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * energyService业务层处理
 *
 * @author cjie
 * @date 2021-10-12
 */
@Service
public class NyTransLoadServiceImpl implements INyTransLoadService {
    @Autowired
    private NyTransLoadMapper nyTransLoadMapper;

    @Autowired
    private NyTransMapper nyTransMapper;

    /**
     * 查询energy
     *
     * @param id energyID
     * @return energy
     */
    @Override
    public NyTransLoad selectNyTransLoadById(String id) {
        return nyTransLoadMapper.selectNyTransLoadById(id);
    }

    /**
     * 查询energy列表
     *
     * @param nyTransLoad energy
     * @return energy
     */
    @Override
    public List<NyTransLoad> selectNyTransLoadList(NyTransLoad nyTransLoad) {
        return nyTransLoadMapper.selectNyTransLoadList(nyTransLoad);
    }

    /**
     * 新增energy
     *
     * @param nyTransLoad energy
     * @return 结果
     */
    @Override
    public int insertNyTransLoad(NyTransLoad nyTransLoad) {
        nyTransLoad.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        return nyTransLoadMapper.insertNyTransLoad(nyTransLoad);
    }

    /**
     * 修改energy
     *
     * @param nyTransLoad energy
     * @return 结果
     */
    @Override
    public int updateNyTransLoad(NyTransLoad nyTransLoad) {
        return nyTransLoadMapper.updateNyTransLoad(nyTransLoad);
    }

    /**
     * 批量删除energy
     *
     * @param ids 需要删除的energyID
     * @return 结果
     */
    @Override
    public int deleteNyTransLoadByIds(String[] ids) {
        return nyTransLoadMapper.deleteNyTransLoadByIds(ids);
    }

    /**
     * 删除energy信息
     *
     * @param id energyID
     * @return 结果
     */
    @Override
    public int deleteNyTransLoadById(String id) {
        return nyTransLoadMapper.deleteNyTransLoadById(id);
    }

    @Override
    public NyTransLoadWXVO selectNyTransLoadByentity(NyTransLoad nyTransLoad) {
        NyTrans nyTrans = nyTransMapper.selectNyTransVoById(nyTransLoad.getTransId());
        //NyTransLoadWXVO result = POJOConverter.copyObject(nyTrans,NyTransLoadWXVO.class);
        NyTransLoadWXVO result = new NyTransLoadWXVO();
        result.setTransId(nyTrans.getId());
        result.setTransName(nyTrans.getName());
        result.setTransNum(nyTrans.getNum());
        result.setInstallPosition(nyTrans.getInstallPosition());
        result.setApprovalLoad(nyTrans.getApprovalLoad());
        result.setCheckTime(nyTransLoad.getCheckTime());
        nyTransLoad.setValue(null);
        List<NyTransLoad> nyTransLoads = this.selectNyTransLoadList(nyTransLoad);
        if(nyTransLoads != null && nyTransLoads.size()>0){
            result.setId(nyTransLoads.get(0).getId());
            result.setCheckTime(nyTransLoads.get(0).getCheckTime());
            result.setValue(nyTransLoads.get(0).getValue());
        }
        return result;
    }

    @Override
    public int addAndUpdateTransLoad(NyTransLoad nyTransLoad) {
        int result =0;

        List<NyTransLoad> nyTransLoads = this.selectNyTransLoadList(nyTransLoad);
        if(nyTransLoads != null && nyTransLoads.size()>0){
            nyTransLoad.setId(nyTransLoads.get(0).getId());
            result = this.updateNyTransLoad(nyTransLoad);
        }else {
            result = this.insertNyTransLoad(nyTransLoad);
        }
        return result;
    }

    @Override
    public List<NyTransLoad> selectNyTransLoadListPlus(NyTransLoad nyTransLoad) {
        return nyTransLoadMapper.selectNyTransLoadListPlus(nyTransLoad);
    }

    @Override
    public List<NyTransLoad> selectNyTransLoadTrend(NyTransLoad nyTransLoad) {
        return nyTransLoadMapper.selectNyTransLoadTrend(nyTransLoad);
    }
}
